/*
 * StyleGenerator.java
 *
 *  created: 24.8.2011
 *  charset: UTF-8
 *  license: MIT (X11) (See LICENSE file for full license)
 */
package cz.mp.k3bg.core;

import cz.mp.k3bg.Application;
import static cz.mp.k3bg.Application.EOL;
import cz.mp.k3bg.log.LoggerManager;
import cz.mp.util.DateUtils;
import cz.mp.util.FileUtils;
import cz.mp.util.ResourceUtils;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.logging.Logger;

/**
 * Třída {@code StyleGenerator} vytváří soubor soubor stylů,
 * používaný ve vygenerovaných souborech 
 * {@code toc, colophon, personal_notes}.
 * <p>
 * Viz <tt>/resources/files/css/default_template.css</tt>
 * 
 * @author Martin Pokorný
 * @version 0.1
 */
public class StyleGenerator extends Generator {

    private static final boolean DEBUG = false;
    private static final Logger logger =
            LoggerManager.getLogger(StyleGenerator.class, DEBUG);
    
    private static final String DEFAULT_STYLE_TEMPLATE_RES = 
            "/resources/files/css/default_template.css";
    
    // -----
    
    public StyleGenerator(BookFiles bookFiles) {
        super(bookFiles, null);
    }
    
//    public StyleGenerator(BookFiles bookFiles, Metadata metadata) {
//        super(bookFiles, metadata);
//    }

    // -----
        
    /**
     * 
     * @throws IOException 
     */
    @Override
    public void generate() throws IOException {
        logger.info("");
        StyleFile styleFile = bookFiles.getBaseStyle();
        logger.config("styleFile = " + styleFile);
        
        BufferedWriter writer;
        if (styleFile.getFile().exists() && 
                !styleFile.getFile().isDirectory()) {
            // připojit na konec souboru, pokud existuje !
            writer = styleFile.getBufferedWriter(true);
            logger.config("append to end");
        }
        else {
            writer = styleFile.getBufferedWriter(false);
            logger.config("new file");
        }

        writer.write("/* ************************************* " 
                + Application.NAME_SHORT 
                + "  (" + DateUtils.getFullCurrentDate() + ")"
                + " */");
        writer.write(EOL);
//        writer.write("/* charset: " + styleFile.getCharsetName() + " */");
        
        String ssTemplate = ResourceUtils
                .readFileFromResource(DEFAULT_STYLE_TEMPLATE_RES);
        logger.finer("template = " + DEFAULT_STYLE_TEMPLATE_RES);
        
        // nahrazení jmen v šabloně za skutečná jména třídy 
        for (StyleClassNames sClass : StyleClassNames.values()) {
            ssTemplate = ssTemplate.replaceAll(
                    sClass.name(), 
                    sClass.getRealName());
        }
        writer.write(ssTemplate);
        
        writer.flush();
        writer.close();
        logger.fine("(finished!)");
    }

    /**
     * Vytvoří výchozí styl v zadaném adresáři,
     * pokud soubor takového jména už neexistuje.
     *
     * @param directory
     * @throws IOException
     */
    public static void generateDefault(String directory) throws IOException {
        if (FileUtils.isDirectory(directory)) {
            throw new IllegalStateException(directory + "  isn't directory");
        }
        BookFiles bf = new BookFiles(directory);

        bf.setDefaultBaseStyle();
        if (! bf.getBaseStyle().exists()) {
//            bf.getBaseStyle().delete();
            StyleGenerator sg = new StyleGenerator(bf);
            sg.generate();
        }

    }
            
//    /** */
//    public static void main(String[] args) throws URISyntaxException, IOException {
//        StyleFile css = new StyleFile("style_out.css", "UTF-8");
//        
//        BookFiles bookFiles = new BookFiles();
//        bookFiles.setBaseStyle(css);
//        
//        StyleGenerator sg = new StyleGenerator(bookFiles);
//        css.delete();
//        
//        sg.generate();
//        System.out.println(
//                css.readFileToString()
//                );
//    }
}   // StyleGenerator.java

